﻿Imports System.Data.OleDb

Public Class CricFixture
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
            BindComments()
        End If
    End Sub

    Protected Sub btnAddComment_Click(sender As Object, e As EventArgs) Handles btnAddComment.Click
        Response.Redirect("AddToBlog/AddAnswers.aspx?ID=" + Request.QueryString("ID"))
    End Sub
    Private Sub BindComments()


        Dim sqlQueryForeRole As String = "SELECT CricQuestionAnswers.ID, CricQuestionAnswers.Title, CricQuestionAnswers.UserAnswers, CricQuestionAnswers.CreatedBy, CricQuestionAnswers.AnsweredDate, Count(CricAnswersLike.CricQuestionID) AS Likes FROM CricQuestionAnswers LEFT JOIN CricAnswersLike ON CricQuestionAnswers.ID = CricAnswersLike.CricQuestionID WHERE (((CricQuestionAnswers.CricQuestionID)=[@f1]))  GROUP BY CricQuestionAnswers.ID, CricQuestionAnswers.Title, CricQuestionAnswers.UserAnswers, CricQuestionAnswers.CreatedBy, CricQuestionAnswers.AnsweredDate"
        Dim p1 As New OleDbParameter("@f1", Request.QueryString("ID"))
        Dim ds = DataAccessLayer.ExecuteDataSet(sqlQueryForeRole, p1)
        gvComments.DataSource = ds
        gvComments.DataBind()
    End Sub

    Protected Sub gvComments_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvComments.RowDeleting
        Dim s As String = String.Empty
        Dim i As Integer
        If (User.Identity.Name.Length > 0) Then
            Dim id As String = gvComments.DataKeys(e.RowIndex).Value.ToString
            Dim p1 As New OleDbParameter("@f1", id)
            Dim usernameP As New OleDbParameter("@f2", User.Identity.Name)




            Using oledbReader As OleDbDataReader = DataAccessLayer.ExecuteDataReader("Select count(*) from CricAnswersLike where CricQuestionID= @f1 and Username=@f2", p1, usernameP)
                While oledbReader.Read
                    If Not IsDBNull(oledbReader.Item(0)) Then
                        i = Convert.ToInt32(oledbReader.Item(0))
                    End If
                End While
            End Using


            If i = 0 Then
                Dim sql2 As String = "insert into CricAnswersLike (CricQuestionID,Username,UserLike) values (@f1,@f2,@f3)"
                Dim p3 As New OleDbParameter("@f3", "True")
                DataAccessLayer.ExecuteNonQuery(sql2, p1, usernameP, p3)
                Dim sql21 As String = "Update userprofile set CommentsLikedByOthers= CommentsLikedByOthers+1 where username =@f2"
                DataAccessLayer.ExecuteNonQuery(sql21, usernameP)
            Else
                Dim sql1 As String = "Delete * from CricAnswersLike where CricQuestionID=@f1 and Username=@f2"
                DataAccessLayer.ExecuteNonQuery(sql1, p1, usernameP)

                Dim sql21 As String = "Update userprofile set CommentsLikedByOthers= CommentsLikedByOthers-1 where username =@f2"
                DataAccessLayer.ExecuteNonQuery(sql21, usernameP)

            End If
            BindComments()
        Else
            Response.Redirect("~/Account/Login.aspx")
        End If

    End Sub

End Class